package com.fgw.admin.web;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.fgw.admin.config.ShiroRealm;
import com.fgw.admin.dto.ResultInfo;
import com.fgw.admin.dto.UserInfo;
import com.fgw.admin.entity.Organization;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import java.util.*;
import javax.annotation.Resource;
import org.springframework.stereotype.Controller;

import com.fgw.admin.service.IOrganizationService;
import com.fgw.admin.vo.IndustryVo;

/**
 *  前端控制器
 *
 * @author system
 * @date 2020-11-09 22:20:25
 */
@Controller
@RequestMapping("/organization")
public class OrganizationController {
    @Resource
    private IOrganizationService iOrganizationService;

    @RequestMapping("/*")
    public void toHtml(){

    }

    @RequestMapping("/listData")
    @RequiresPermissions("organization:view")
    public @ResponseBody
    ResultInfo<List<Organization>> listData(Organization organization, Integer page, Integer limit){
        EntityWrapper<Organization> wrapper = new EntityWrapper<>(organization);
//        Page<Organization> pageObj = iOrganizationService.selectPage(new Page<>(page,limit), wrapper);
       List<Organization> list = iOrganizationService.selectList(wrapper);
        return new ResultInfo<>(list);
    }

    @RequestMapping("/add")
    @RequiresPermissions("organization:add")
    public @ResponseBody
    ResultInfo<Boolean> add(Organization organization){
    	UserInfo info = ShiroRealm.getCurrentUser();
    	//生成组织编码
    	organization.setOrgCode("org-"+System.currentTimeMillis()/1000);
    	organization.setCreateTime(new Date());
    	organization.setCreateUser(info.getName());
    	organization.setCreateUserid(info.getId());
        boolean b = iOrganizationService.insert(organization);
        return new ResultInfo<>(b);
    }

    @RequestMapping("/delBatch")
    @RequiresPermissions("organization:del")
    public @ResponseBody
    ResultInfo<Boolean> delBatch(Integer[] idArr){
        boolean b = iOrganizationService.deleteBatchIds(Arrays.asList(idArr));
        return new ResultInfo<>(b);
    }

    @RequestMapping("/edit")
    @RequiresPermissions("organization:edit")
    public @ResponseBody
    ResultInfo<Boolean> edit(Organization organization){
    	UserInfo info = ShiroRealm.getCurrentUser();
    	organization.setUpdateTime(new Date());
    	organization.setUpdateUser(info.getName());
    	organization.setUpdateUserid(info.getId());
        boolean b = iOrganizationService.updateById(organization);
        return new ResultInfo<>(b);
    }

    @RequestMapping(value="/getOrgTree",method=RequestMethod.POST)
    @ResponseBody
    public ResultInfo<List<IndustryVo>> getOrgTree(){
    	
    	List<IndustryVo> list = iOrganizationService.getOrgTree();
    	
    	return new ResultInfo<>(list);
    	
    	
    }
}
